// pages/scoreMall/scoreOrder/scoreOrder.js
const orderService = require('../../../api/orderService.js')

//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    member: {},
    noMore: false,
    loading: false,
    page: 1,
    pageSize: 20,
    pageCount: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.userInfo && app.globalData.userInfo.member) {
      this.setData({
        member: app.globalData.userInfo.member
      })
      this.getOrderData(this.data.page)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      orderList: [],
      member: {},
      page: 1,
      pageCount: 1,
      noMore: false
    })
    this.onLoad();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page < this.data.pageCount) {
      // 如果查询结果数量不为0，说明还有下一页，执行查询操作
      this.setData({
        loading: true
      })
      let page = this.data.page;
      ++page;
      this.getOrderData(page);
    } else {
      // 没有下一页，提示已经查询完毕
      this.setData({
        noMore: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取订单数据
   */
  getOrderData: function (page) {
    const that = this
    orderService.query(page, this.data.pageSize, this.data.member.id, 'SCORE').then(res => {
      console.log(res)
      let tempList = that.data.orderList
      if (res.records && res.records.length > 0) {
        res.records.forEach(item => {
          let tempItem = {
            orderNum: item.id,
            status: item.status,
            business: item.business,
            storeName: item.storeName, //发货门店名称
            storeId: item.storeId,
            shipmentAmount: item.shipmentAmount >= 0 ? (item.shipmentAmount).toFixed(2) : 0, //运费
            details: [],
            buyNumber: item.buyNumber ? item.buyNumber : 0,
            totalPrice: item.cashTotal,
            exchangeScores: item.images.length > 0 ? item.images[0].exchangeScores : ''
          }
          if (item.images) {
            item.images.forEach((i) => {
              tempItem.count += parseInt(i.goodsNum);
              tempItem.details.push({
                id: i.productId,
                img: i.url,
                goodsName: i.goodsName,
                goodsNum: i.productNum,
              });
            })
          } else {
            tempItem.count = 0
          }
          tempList.push(tempItem)
        });

        let pageCount = parseInt(res.recordCount / res.pageSize) + 1;
        if (res.recordCount % res.pageSize === 0) {
          pageCount = parseInt(res.recordCount / res.pageSize);
        }
        that.setData({
          page: res.page,
          pageCount: pageCount,
          orderList: tempList
        })
      }
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
      if (this.data.loading) {
        that.setData({
          loading: false
        })
      }
    })
  },
  /**
   * 前往订单详情
   */
  toOrderDetails: function (val) {
    console.log(val)
    wx.navigateTo({
      url: '../scoreOrderDetails/scoreOrderDetails?orderId=' + val.currentTarget.dataset.id,
    })
  },
})